<?php
/**
 * 
 * 操作文章
 * 
 * @category System
 * @author Steve Liu
 * @copyright Eshifang 
 */

class Web_Model_Article
{
	/**
	 * 默认数据库名
	 * 
	 * @var string
	 */
    private static $_table = 'article';
    
    /**
     * 获得文章的数量
     * 
     * @param int|string $filed
     * @param int|string $value
     * @return int
     */
    private static function _getCount($filed = 1, $value = 1)
    {
        return Eren_Registry::get('DB')->table(self::$_table)
        							   ->where($filed, '=', $value)
        							   ->wAnd()
        							   ->where('article_type', '=', 1)
        							   ->count();
    }
    
    /**
     * 获得文章列表
     * 
     * @param int $start
     * @param int $limit
     * @param int|string $field
     * @param int|string $value
     * @return array
     */
    private static function _getList($start=0, $limit=100, $field=1, $value=1)
    {
    	$start = intval($start);
    	$limit = intval($limit);
    	$list = array('article_id',
    			       'article_main_class_id',
    			       'article_main_class_name',
    			       'article_sub_class_id',
    			       'article_sub_class_name',
    			       'article_author_id',
    			       'article_author_name',
    			       'article_title',
    			       'article_time'
    	);
        return Eren_Registry::get('DB')->table(self::$_table)
	                   				   ->where('article_type', '=', 1)
	                   				   ->wAnd()
	                   				   ->where($field, '=', $value)
	                   				   ->orderBy('article_id', 'DESC')
	                   				   ->limit($start, $limit)
	                   				   ->select($list);
    }
    
    /**
     * 获得所有的文章列表
     * 
     * @param int $start
     * @param int $limit
     * @return array
     */
    public static function getListAll($start=0, $limit=100)
    {
    	$articles = self::_getList($start, $limit);
    	$articles['total'] = self::_getCount();
        return $articles;
    }
    
    /**
     * 获得主分类的文章列表
     * 
     * @param int $ID
     * @param int $start
     * @param int $limit
     * @return array
     */
    public static function getListByMainClassID($ID, $start=0, $limit=100)
    {
    	$ID = intval($ID);
    	$articles = self::_getList($start, $limit, 'article_main_class_id', $ID);
        $articles['total'] = self::_getCount('article_main_class_id', $ID);
        return $articles;
    }
    
    /**
     * 获得子分类的文章列表
     * 
     * @param int $ID
     * @param int $start
     * @param int $limit
     * @return array
     */
    public static function getListBySubClassID($ID, $start=0, $limit=100)
    {
        $ID    = intval($ID);
        $ID = intval($ID);
    	$articles = self::_getList($start, $limit, 'article_sub_class_id', $ID);
        $articles['total'] = self::_getCount('article_sub_class_id', $ID);
        return $articles;
    }
    
    /**
     * 获得某篇文章的详情
     * 
     * @param int $ID
     * @return array
     */
    public static function getByID($ID)
    {
        $ID = intval($ID);
        $article = Eren_Registry::get('DB')->table(self::$_table)
						                   ->where('article_id', '=', $ID)
						                   ->wAND()
						                   ->where('article_type', '=', 1)
						                   ->select();
        return !isset($article['data'][0])? array():$article['data'][0];
    }
    
    /**
     * 获得上篇文章的标题和ID
     * 
     * @param int $ID
     * @return array
     */
    public static function getLastArticleByID($ID)
    {
    	$ID = intval($ID);
	    $article = Eren_Registry::get('DB')->table(self::$_table)
						                   ->where('article_id', '<', $ID)
						                   ->wAND()
						                   ->where('article_type', '=', 1)
						                   ->orderBy('article_id', 'DESC')
						                   ->limit(0, 1)
						                   ->select(array('article_id', 'article_title'));
        return !isset($article['data'][0])? array():$article['data'][0];
    }
    
    /**
     * 获得下篇文章的标题和ID
     * 
     * @param int $ID
     * @return array
     */
	public static function getNextArticleByID($ID)
    {
    	$ID = intval($ID);
	    $article = Eren_Registry::get('DB')->table(self::$_table)
						                   ->where('article_id', '>', $ID)
						                   ->wAND()
						                   ->where('article_type', '=', 1)
						                   ->orderBy('article_id', 'ASC')
						                   ->limit(0, 1)
						                   ->select(array('article_id', 'article_title'));
        return !isset($article['data'][0])? array():$article['data'][0];
    }
    
    /**
     * 校验插入字段是否合法
     * 
     * @param string $title
     * @param int $mainClassID
     * @param int $subClassID
     * @param string $article
     * @throws Eren_Exception
     */
    private static function _check($title, $mainClassID, $subClassID, $article)
    {
        if(!Web_Model_Validator::checkArticleTitle($title)){
            throw new Eren_Exception('标题内容不合法，必须是1到50个汉字', -1);
        }else if($mainClassID == -1){
            throw new Eren_Exception('父类不可为空', -2);
        }else if($subClassID == -1){
            throw new Eren_Exception('子类不可为空', -3);
        }else if(!Web_Model_Class::checkClass($mainClassID, $subClassID)){
            throw new Eren_Exception('分类选择不合法，请选择正确的分类', -4);
        }else if(empty($article)){
            throw new Eren_Exception('文章内容不可为空', -5);
        }
    }
    
    /**
     * 格式化插入的数据
     * 
     * @param string $title
     * @param int $mainClassID
     * @param int $subClassID
     * @param string $article
     * @return array
     */
    private static function _format($title, $mainClassID, $subClassID, $article)
    {
        $fields = array();
        $fields['article_title']           = $title;
        $fields['article_main_class_id']   = $mainClassID;
        $fields['article_main_class_name'] = Web_Model_Class::getClassNameByID($mainClassID);
        $fields['article_sub_class_id']    = $subClassID;
        $fields['article_sub_class_name']  = Web_Model_Class::getClassNameByID($subClassID);
        $fields['article_author_id']       = Web_Model_Member::getID();
        $fields['article_author_name']     = Web_Model_Member::getNickName();
        $fields['article_content']         = $article;
        $fields['article_type']            = 1;
        return $fields;
    }
    
    /**
     * 执行插入操作
     * 
     * @param string $title
     * @param int $mainClassID
     * @param int $subClassID
     * @param string $article
     * @throws Eren_Exception
     * @return int
     */
    public static function insert($title, $mainClassID, $subClassID, $article)
    {
        if(!Web_Model_Auth::hasRightAdd()){
            throw new Eren_Exception('您没有权限发表文章', -6);
        }
        $title        = Eren_Filter::htmlspecialchars($title);
        $mainClassID  = intval($mainClassID);
        $subClassID   = intval($subClassID);
        $article      = Eren_Filter::addslashes($article);
        self::_check($title, $mainClassID, $subClassID, $article);
        $fields = self::_format($title, $mainClassID, $subClassID, $article);
        $fields['article_time'] = Web_Model_Common::getNowTime();
        $ret = Eren_Registry::get('DB')->table('article')->insert($fields);
        return $ret;
    }
    
    /**
     * 执行更新操作
     * 
     * @param string $title
     * @param int $mainClassID
     * @param int $subClassID
     * @param string $article
     * @throws Eren_Exception
     */
    public static function update($articleID, $title, $mainClassID, $subClassID, $article)
    {
        $articleID    = intval($articleID);
        $articleData = Web_Model_Article::getByID($articleID);
        if(empty($articleData)){
            throw new Eren_Exception('文章ID不合法', -6);
        }else if(!Web_Model_Auth::hasRightUpdateArticle($articleData['article_author_id'])){
            throw new Eren_Exception('您没有权限修改这篇文章', -7);
        }
        $title        = Eren_Filter::htmlspecialchars($title);
        $mainClassID  = intval($mainClassID);
        $subClassID   = intval($subClassID);
        $article      = Eren_Filter::addslashes($article);
        self::_check($title, $mainClassID, $subClassID, $article);
        $fields = self::_format($title, $mainClassID, $subClassID, $article);
        Eren_Registry::get('DB')->table('article')->where('article_id', '=', $articleID)->update($fields);
        return TRUE;
    }
    
    /**
     * 执行删除操作
     * 
     * @param int $articleID
     * @throws Eren_Exception
     */
    public static function delete($articleID)
    {
        $articleID    = intval($articleID);
        $articleData = Web_Model_Article::getByID($articleID);
        if(empty($articleData)){
            throw new Eren_Exception('文章ID不合法', -6);
        }else if(!Web_Model_Auth::hasRightUpdateArticle($articleData['article_author_id'])){
            throw new Eren_Exception('您没有权限删除这篇文章', -7);
        }
        Eren_Registry::get('DB')->table('article')->where('article_id', '=', $articleID)->update(array('article_type'=>2));
        return TRUE;
    }
}
